#include<iostream>
#include<cstring>
using namespace std;
const int N = 1e3 + 10;
int f[N];
int l[N], h[N], v[N], w[N];
int n, m;
int main()
{
	cin >> m >> n;
	cin >> v[0] >> w[0];
	for (int i = 1; i <= n; i++) {
		cin >> l[i] >> h[i] >> v[i] >> w[i];
	}
	for (int i = v[0]; i <= m; i++) {
		f[i] = max(f[i], f[i - v[0]] + w[0]);
	}
	for (int i = 1; i <= n; i++) {
		for (int j = m; j >= 0; j--) {
			for (int k = 0; k <= l[i] / h[i]; k++) {
				if (j >= k * v[i]) {
					f[j] = max(f[j], f[j - k * v[i]] + k * w[i]);
				}
			}
		}
	}
	cout << f[m] << endl;
	return 0;
}